<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class AdminUser extends Model
{
    //登陆功能
    public function userLogin($data){
     try{
            //查询用户输入账号的数据 mysql
            $username=$this->where('username',$data['username'])->find();
            if(!isset($username)){
                throw new \Exception("该用户未注册");
            }else{
                if($data['password']!== $username->password){
                    throw new \Exception("密码错误");
                }
                $updateData= [
                    'token' => $data['token'],
                    'token_out' => $data['token_out']
                ];
                 //插入token  mysql
                $this->where('id',$username->id)->update($updateData);
                return json([
                'code' => 20000,
                'data' =>['token'=>$data['token']],
                ]);
            }   
        }catch(\Exception $e){
            return  json([
                'code' => 410,
                'data' => $e->getMessage()
            ]);
        }
    }
    
    //
    public function getLogin($token){
        try{
            $username=$this->where('token',$token)->find();
            if(!isset($username)){
                throw new \Exception("token is error");
            }
            $arr=$username->adminUserPermission;
            $arr2=[];
            foreach ($arr as $role){
                	$arr2[]=$role->p_key;
            }
            $arr2[]='test';
           $msgData= [
                    'username' =>$username->username,
                    'token' => $username->token,
                    'token_out' => $username->token_out,
                    'avatar'=>"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
                   'introduction'=> "I am a super administrator",
                    'name'=>"Super Admin",
                    'roles'=>$arr2
                ];
            return json([
                'code'=> 20000,
                'data'=>$msgData
            ]);
        }catch(\Exception $e){
            return  json([
                'code' => 410,
                'data' => $e->getMessage()
            ]);
            
        }
        
    }
    //
    public function getAdminUser(){
        // $data=$this->column('username');
        $data=$this->select()->toArray();
        $arr=[];

        $l=count($data);
        for($i=0;$i<=$l-1;$i++){
            $arr[]=['id'=>$data[$i]['id'],'username'=>$data[$i]['username']];
        }
        return json([
                'code'=> 20000,
                'data'=>$arr
            ]);
    }
    //
    public function registerAdminuser($data){
       $res= $this->save($data);
        return json([
                'code'=> 20000,
                'data'=>$res
            ]);
        
    }
    //
    public function adminUserPermission()
    {
        // return $this->hasMany(AdminUserPermission::class,'user_id');
         return $this->belongsToMany(AdminPermission::class, AdminUserPermission::class,'permission_id','user_id');
    }
    public function updateAdminUserPermission()
    {
        // return $this->hasMany(AdminUserPermission::class,'user_id');
         return $this->hasMany(AdminUserPermission::class,'user_id');
    }
    public function permission($id){
       return  $this->find($id)->adminUserPermission;

    }
    
}
